//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CubicBSplinesFitting.h"
using namespace Cephei::QL::Termstructures::Yield;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::CCubicBSplinesFitting (Cephei::Core::IVector<Double>^ knotVector, Microsoft::FSharp::Core::FSharpOption<Boolean>^ constrainAtZero) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phCubicBSplinesFitting = NULL;
#endif
        knotVector->Lock();
        INativeVector<Double>^ _NCIknotVector = knotVector->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCknotVector = safe_cast<CDoubleVector^>(_NCIknotVector);
        std::vector<QuantLib::Time>& _knotVector = static_cast<std::vector<QuantLib::Time>&> (_NCknotVector->GetReference ());
        bool _constrainAtZero = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (constrainAtZero) ? (bool)ValueHelper::Convert (constrainAtZero->Value) : true); //4
        _ppCubicBSplinesFitting = new boost::shared_ptr<QuantLib::CubicBSplinesFitting> (new QuantLib::CubicBSplinesFitting ( _knotVector,  _constrainAtZero ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (knotVector != nullptr) knotVector->Unlock();    //not optional
    }
}
Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::CCubicBSplinesFitting (boost::shared_ptr<QuantLib::CubicBSplinesFitting>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCubicBSplinesFitting = NULL;
#endif
	_ppCubicBSplinesFitting = &childNative;
    
}
Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::CCubicBSplinesFitting (QuantLib::CubicBSplinesFitting& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCubicBSplinesFitting = NULL;
#endif
	_ppCubicBSplinesFitting = new boost::shared_ptr<QuantLib::CubicBSplinesFitting> (&childNative);
    
    _CubicBSplinesFittingOwner = owner;
    
}

Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::CCubicBSplinesFitting (CCubicBSplinesFitting^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCubicBSplinesFitting = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCubicBSplinesFitting = new boost::shared_ptr<QuantLib::CubicBSplinesFitting> (copy->GetShared());
        
    }
}
Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::CCubicBSplinesFitting (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCubicBSplinesFitting = NULL;
#endif
	if (!t->IsSubclassOf(CCubicBSplinesFitting::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::CCubicBSplinesFitting (QuantLib::Handle<QuantLib::CubicBSplinesFitting>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCubicBSplinesFitting = &childNative;
	_ppCubicBSplinesFitting = &static_cast<boost::shared_ptr<QuantLib::CubicBSplinesFitting>>(childNative.currentLink());
    
    _CubicBSplinesFittingOwner = owner;
}
Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::CCubicBSplinesFitting (QuantLib::Handle<QuantLib::CubicBSplinesFitting> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCubicBSplinesFitting = &childNative;
	_ppCubicBSplinesFitting = &static_cast<boost::shared_ptr<QuantLib::CubicBSplinesFitting>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::CCubicBSplinesFitting (QuantLib::CubicBSplinesFitting childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCubicBSplinesFitting = NULL;
#endif
	_ppCubicBSplinesFitting = new boost::shared_ptr<QuantLib::CubicBSplinesFitting> (new QuantLib::CubicBSplinesFitting (childNative));
    
}
#endif

Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::~CCubicBSplinesFitting ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCubicBSplinesFitting != NULL)
    {
	    delete _ppCubicBSplinesFitting;
        _ppCubicBSplinesFitting = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::!CCubicBSplinesFitting ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCubicBSplinesFitting != NULL)
    {
	    delete _ppCubicBSplinesFitting;
    }
}
QuantLib::CubicBSplinesFitting& Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::GetReference ()
{
    if (_ppCubicBSplinesFitting == NULL) throw REFNEW NativeNullException ();
	return **_ppCubicBSplinesFitting;
}
boost::shared_ptr<QuantLib::CubicBSplinesFitting>& Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::GetShared ()
{
    if (_ppCubicBSplinesFitting == NULL) throw REFNEW NativeNullException ();
	return *_ppCubicBSplinesFitting;
}
QuantLib::CubicBSplinesFitting* Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::GetPointer ()
{
    if (_ppCubicBSplinesFitting == NULL) throw REFNEW NativeNullException ();
	return &**_ppCubicBSplinesFitting;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CubicBSplinesFitting>& Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::GetHandle ()
{
	if (_phCubicBSplinesFitting == NULL)
	{
		_phCubicBSplinesFitting = new Handle<QuantLib::CubicBSplinesFitting> (*_ppCubicBSplinesFitting);
	}
	return *_phCubicBSplinesFitting;
}
#endif
bool Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::HasNative () 
{
	return (_ppCubicBSplinesFitting != NULL);
}

Double Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting::BasisFunction (Int32 i, Double t)
{
    try
    {
        QuantLib::Integer _i = (QuantLib::Integer)ValueHelper::Convert (i); //a
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCubicBSplinesFitting)->basisFunction ( _i,  _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::ICubicBSplinesFitting^ Cephei::QL::Termstructures::Yield::CCubicBSplinesFitting_Factory::Create (Cephei::Core::IVector<Double>^ knotVector, Microsoft::FSharp::Core::FSharpOption<Boolean>^ constrainAtZero)
{
    return REFNEW CCubicBSplinesFitting ( knotVector,  constrainAtZero);
}
